package org.scboy.rbac.controller;

import java.util.Calendar;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.LoginSessionService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rbac/loginsession")
@Menu("权限管理系统.会话管理")
public class LoginSessionController {

	@Autowired
	LoginSessionService loginSessionService;
	
	@PostMapping("page")
	@Permission("分页查询")
	public Result page(@RequestBody Model params){
		Page page=loginSessionService.page(params);
		return Result.Success(page.getData(),page.getCount());
	}
	
	/**
	 * 关闭会话成功返回RBAC_SESSION_CLOSE_SUCCESS
	 * 关闭会话失败返回RBAC_SESSION_CLOSE_ERROR。
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("closesession")
	@Permission("强制关闭登陆会话")
	public Result closeSession(String id){
		if(loginSessionService.adminClose(id)){
			return Result.Success("RBAC_SESSION_CLOSE_SUCCESS");
		}else{
			return Result.Error("RBAC_SESSION_CLOSE_ERROR");
		}
	}
	
	/**
	 * 清除类型:
	 * 		1、清除7天之前的会话
	 *		2、清除15天之前会话
	 *		3、清除一个月之前会话
	 *		4、清除3个月之前会话
	 *		5、清除一年之前会话。
	 * 清空会话成功返回RBAC_SESSION_CLEARN_SUCCESS
	 * 清空会话失败返回RBAC_SESSION_CLEARN_ERROR。
	 * @param request
	 * @param type
	 * @return
	 */
	@DeleteMapping("clearn")
	@Permission("清理会话日志")
	public Result clearn(int type){
		Calendar cal=Calendar.getInstance();
		if(type==1){
			cal.add(Calendar.DAY_OF_WEEK, -7);
		}else if(type==2) {
			cal.add(Calendar.DAY_OF_WEEK, -15);
		}else if(type==3) {
			cal.add(Calendar.MONTH, -1);
		}else if(type==4) {
			cal.add(Calendar.MONTH, -3);
		}else if(type==5) {
			cal.add(Calendar.YEAR, -1);
		}
		loginSessionService.delete(cal.getTime());
		return Result.Success("RBAC_SESSION_CLEARN_SUCCESS");
	}

}
